/*
 * DTOBackground.cpp
 *
 *  Created on: Oct 23, 2011
 *      Author: gonzalo
 */

#include "DTOBackground.h"

DTOBackground::DTOBackground(std::string& aDeserealizar, std::string owner) :
		DTO(BACKGROUND, owner) {
	deserializar(aDeserealizar);
}

DTOBackground::DTOBackground(std::string imagenPath, bool obviado) :
		DTO(BACKGROUND, SERVER_OWN) {
	this->imagenPath = imagenPath;
	this->leido = NULL;
}
DTOBackground::~DTOBackground() {
	if (this->leido != NULL) {
		free(leido);
	}
}

std::string DTOBackground::serializar() {

	if (this->leido == NULL) {
		this->getStringImagen(imagenPath);
	}

	std::string imagenStr;
	char* constLeido = leido;

	for (int var = 0; var < imageSize; ++var) {
		if (constLeido[var] == NULL) {
			nullCharacters.push_back(var);
			constLeido[var] = 'R'; //RIP Dennis Ritchie
		}
	}

	imagenStr.append(constLeido, imageSize);

	std::string extension = imagenPath.substr(imagenPath.find_last_of('.') + 1);

	std::stringstream stremRetorno;
	stremRetorno
			<< UtilString::TOlenghtStringStringValue(nullCharacters.size());
	for (std::list<int>::iterator it = nullCharacters.begin();
			it != nullCharacters.end(); ++it) {
		stremRetorno << UtilString::TOlenghtStringStringValue(*it);
	}
	stremRetorno << UtilString::TOlenghtStringStringValue(extension);
	stremRetorno << UtilString::TOlenghtStringStringValue(imageSize);
	stremRetorno << UtilString::TOlenghtStringStringValue(imagenStr);

	return stremRetorno.str();

}
void DTOBackground::deserializar(std::string& string) {
	int nullCharactersQuantity = UtilString::stringToInt(
			UtilString::FROMlenghtStringStringValue(string));
	nullCharacters.clear();
	int nullCharacterPosicion;
	for (int var = 0; var < nullCharactersQuantity; ++var) {
		nullCharacterPosicion = UtilString::stringToInt(
				UtilString::FROMlenghtStringStringValue(string));
		nullCharacters.push_back(nullCharacterPosicion);
	}
	this->extension = UtilString::FROMlenghtStringStringValue(string);
	this->imageSize = UtilString::stringToInt(
			UtilString::FROMlenghtStringStringValue(string));
	this->imagen = UtilString::FROMlenghtStringStringValue(string);
}

std::string DTOBackground::getImagePath() {
	std::cout<<"DTOBackground::getImagePath"<<std::endl;
	imagenPath = TEMP_BCGR + '.' + this->extension;

	this->createTempImagen();
	return imagenPath;
}

void DTOBackground::createTempImagen() {
	FILE* file;

	file = fopen(imagenPath.c_str(), "w+");
	if (file == NULL) {
		Logger::log("DTOBackground", "getStringImagen",
				"Fallo al crear la imagen,verificar permisos");
	}
	char* cstr = new char[imagen.size() + 1];strcpy
	(cstr, imagen.c_str());
	for (std::list<int>::iterator it = nullCharacters.begin();
			it != nullCharacters.end(); ++it) {
		cstr[*it] = NULL;
	}
	std::cout<<"creando imagen de "<<imageSize<<" bytes."<<std::endl;

	fwrite(cstr, sizeof(char), this->imageSize, file);

	delete[] cstr;
	fclose(file);
}

void DTOBackground::getStringImagen(std::string image) {
	FILE* file;
	file = fopen(image.c_str(), "r");
	if (file == NULL) {
		Logger::log("DTOBackground", "getStringImagen",
				"Fallo al abrir la imagen");
	}

	fseek(file, 0, SEEK_END);
	imageSize = ftell(file);
	fseek(file, 0, SEEK_SET);

	leido = (char*) malloc(sizeof(char) * (imageSize));

	fread(leido, sizeof(char), imageSize, file);

	fclose(file);
}
